<?php	
	function department_map( $items  ){
		foreach( $items as $k => $item ){
			$t = true;
			for( $i=0; $i < count($items) ;$i++  ){
				if( $item['parent_id'] == $items[$i]['ID'] ){
					$t=false;
					break;
				}
			}
			if( $t ){
				$items[$k]['parent_id'] = '0'; 
			}	
		}
		return department_map_recruise($items);
	}
	
	$dp_ord =1;

	function department_map_recruise( $items, $parent_id='0', $c=0 ){
		global $dp_ord;
		if(!$dp_ord) $dp_ord =1;
		//limit deep
		if( $c > 10 ) return "";
		$html="";
		foreach( $items as $k => $a ){
			if( $a['parent_id'] == $parent_id ){
				$html .= "<tr>
					<td align='center'>".($dp_ord++)."</td>
					<td width='200'>
						<a".($parent_id==0?' style="color:red;font-weight:bold"':'').">".str_repeat(" ─ ─ ",$c)." {$a['title']}</a>
					</td>
					<td align='center'>
					".show_date(DATE_FORMAT,$a['date_created'])."
					</td>
					<td align='center'>".
					 get_user_link($a['created_by_id'],$a['created_by_name'],$a['created_is_deleted'])
					."</td>
					<td>
						{$a['desc']}
					</td>
					<td width='30' align='center'>
						<a onclick=\"load_inframe('".baseUrl()."/Admin/Department/edit?ID={$a['ID']}',{title: ' Sửa phòng ban',auto_hidden: true })\" class='x-edit'></a>
					</td>
					<td width='30' align='center'>
					<a href='#Admin/Department/delete?ID={$a['ID']}' class='x-delete'></a>
					</td>
					</tr>";
			
				unset( $items[$k] );
				$html .= department_map_recruise( $items, $a['ID'], $c+1 );				
			}
		}
		return $html;	
	}
?>	


		<div class="x-list-title">
		<table width="100%" border="0" height="100%">
		<tr>
			<td align="left">
				<h2>Danh sách phòng ban</h2>
			</td>
			<td align="right">	
				<div class="x-list-options">
					<a onclick="load_inframe('<?php _baseUrl();?>/Admin/Department/add',{title: ' Thêm phòng ban mới', auto_hidden: true })" class="x-button x-button-add">Phòng ban mới</a>
				</div>
			</td>
		</tr>
		</table>	
		</div>
				
		<table width="100%" cellpadding="5" class="x-list" >
			<tr>
				<th width="25" align="center">TT</th>
				<th width="140">
					Tên phòng ban
				</th>
				<th width="140">
					Ngày tạo
				</th>
				<th width="100">
					Người tạo
				</th>
				<th>
					Mô tả
				</th>
				<th width="25" colspan="2">Tác vụ</th>
			</tr>
			<?php  _e( department_map( $this->get('departments') )) ;?>
			<tr>
				<td colspan="7"></td>
			</tr>
		</table>
	
